Creating Our Slack Application
Learn to create Slack application with a step-by-step tutorial.
We'll cover the following
For the bot to interact with Slack, we need to set up a Slack application:
1 of 9
2 of 9
3 of 9
4 of 9
5 of 9
6 of 9
7 of 9
8 of 9
9 of 9
The slides are explained as follows:
Navigate to Slack on your browser. Here, you will need to click on the “Create an App” button.
We will then be presented with the following dialog box with sub-items such as “From scratch” and “From an app manifest.”
Choose the “From an app manifest” option. Choose the workspace we created at the beginning of this section and then press “Create App.” Click the “Next” button.
Copy the text from the file presented below:
Paste it onto the screen that is shown. Click the “Next” button.
We will be presented with a summary of the bot's permissions. Click the “Create” button.
This will move us to a page that is called “Basic Information.” Scroll down the page until we get to “App-Level Tokens,” as can be seen in the figure. Click the “Generate Token and Scopes” button.
This will lead us to the following dialog box. Set the token name to
petstore-bot. Provide these scopes in the “Scope” field –connections:writeandauthorizations:read. Now, click “Generate.”We will receive an app-level token. Press the “Copy” button and put the token somewhere for the time being. In a production environment, we want to put this in a secure key store, such as Azure Key Vault or AWS Key Management Service. We will need to put it in a file called the
.envfile that we should never check into a repository. Click the “Done” button.
Note: The key here is for a bot that was deleted right after this screenshot.
In the left menu pane, choose “OAuth and Permissions.” On the screen, click “Install to Workspace.” There is a dialog box that asks for a channel to post as an app. Choose any channel we like and hit “Allow.”
We are now back to “OAuth and Permissions,” but we will see our bot's auth token listed. Hit the “Copy” button and store this where we stored the app token from earlier.
Running the applications#
Here, we are going to use Docker Compose to turn up our Open Telemetry services, Jaeger, Prometheus, and our Petstore application. Once those are running we will use Go to compile and run our ChatOps service that implements the chatbot connected to Slack:
/
Execute these commands.
Turn up the Docker containers:
Once the environment is running, change to the
chapter/11/chatbotdirectory:
We will need to create a
.envfile:
The file should contain our tokens and follow this structure:
These tokens were generated when we set up the Slack app. Press “Ctrl+C” after pasting the file.
Run the ChatOps server with the following command:
We should be able to see the following message printed to standard output:
In the background, there is a demonstration client that is adding pets to the pet store and doing searches for pets (some searches will cause errors). The service is set to Float sampling, so not every call will generate a trace.
We can interact with the pet store by using the CLI application. This will let us add our own pets, delete pets, and search for pets with a filter. That client can be found in the client/cli/petstore directory.
Prometheus metrics can be queried by clicking the link below the “Run” button.
To interact with our ChatOps bot, we need to open Slack and add the bot to a channel. We can do this simply by doing a @PetStore mention in a channel. Slack will ask if we would like to add the bot to the channel.
Once that happens, we can try out various operations. Start by asking the bot for help, as follows:
Let's ask for some help on how we can list some traces:
How about we ask the system to give us five recent traces:
We can also ask about a particular trace:
Note: We cannot directly paste a trace ID copied from list traces. This is because those are hyperlinks; we need to remove the rich text from an ID if we want to directly paste it for show trace.
There are more options for us to play with in the bot. Give them a try. This ChatOps application is just the tip of the iceberg. We can make the ChatOps application more powerful than the one we have here. We can have it display graphs, grab profile information from a pprof dump from the service and give us a link to view it, have it deploy new versions of our application, or roll a version back. Push files to the service by simply dragging them into the Slack window (such as a configuration change). Important events such as alerts can be broadcast to people who are on call by having the Ops service send messages to the ChatOps service, and the use of ChatOps increases the observability of what our service is doing and what operations are being done against the service.
And as a side effect, unlike tools that must be run on a laptop or desktop, Slack and many other chat applications have mobile versions, so we can interact or do emergency operations with our cell phones with no extra cost in development.
Creating Event Handlers
Summary